﻿using Newtonsoft.Json;
using OpenCvSharp;
using Prism.Commands;
using Prism.Mvvm;
using System.Collections.Concurrent;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace PLCSharp.Views.Vision.Camera
{
    public partial class CameraBase : BindableBase
    {
        [Key]
        public Guid ID { get; set; } = Guid.NewGuid();
        private string _Name;
        /// <summary>
        /// Comment
        /// </summary>
        public string Name
        {
            get { return _Name; }
            set
            {
                if (_Name != value)
                {
                    Prompt = "已修改，请保存";
                }
                SetProperty(ref _Name, value);
            }
        }

        private CameraBrand _Brand;
        /// <summary>
        /// Comment
        /// </summary>
        public CameraBrand Brand
        {
            get { return _Brand; }
            set
            {
                SetProperty(ref _Brand, value);
                Prompt = "已修改，请保存";
            }
        }
        private string _Comment;
        /// <summary>
        /// 备注
        /// </summary>
        public string Comment

        {
            get { return _Comment; }
            set
            {
                if (_Comment != value)
                {
                    Prompt = "已修改，请保存";
                }
                SetProperty(ref _Comment, value);
            }
        }

        [Column("Params")]
        public string SerializedParams
        {
            get => JsonConvert.SerializeObject(Params); // 自动序列化
            set => Params = JsonConvert.DeserializeObject<CameraParams>(value); // 自动反序列化

        }
        //-------------------------------------------------------NotMapped--------------------------------------------

        private bool _LogSwitch;
        [JsonIgnore]
        [NotMapped]
        /// <summary>
        /// 日志开关
        /// </summary>
        public bool LogSwitch
        {
            get { return _LogSwitch; }
            set { SetProperty(ref _LogSwitch, value); }
        }
        [JsonIgnore]
        [NotMapped]
        public ConcurrentQueue<string> LogQueue { get; set; } = [];

        public void Log(string log)
        {
            if (LogSwitch)
            { LogQueue.Enqueue(log); }
        }
        private string _Prompt;
        [JsonIgnore]
        [NotMapped]
        /// <summary>
        /// 提示
        /// </summary>
        public string Prompt
        {
            get { return _Prompt; }
            set { SetProperty(ref _Prompt, value); }
        }


        private bool _Connected;
        [JsonIgnore]
        [NotMapped]
        /// <summary>
        /// 连接成功
        /// </summary>
        public bool Connected
        {
            get { return _Connected; }
            set { SetProperty(ref _Connected, value); }
        }

        private CameraParams _Params;
        [JsonIgnore]
        [NotMapped]
        /// <summary>
        /// Comment
        /// </summary>
        public CameraParams Params
        {
            get
            {
                _Params ??= new CameraParams();

                return _Params;
            }
            set
            {


                SetProperty(ref _Params, value);



            }
        }


        //-------------------------------------------------------Method--------------------------------------------
        private DelegateCommand<string> _Command;
        public DelegateCommand<string> Command =>
            _Command ??= new DelegateCommand<string>(ExecuteOpenCommand);

        void ExecuteOpenCommand(string cmd)
        {
            switch (cmd)
            {
                case "Open":
                    Open();
                    break;
                case "Trig":
                    SoftwareTrig();
                    break;
                case "Continuous":
                    Continuous();
                    break;
                case "StopContinuous":
                    StopContinuous();
                    break;
            }

        }

        public virtual bool Open() { return false; }

        public virtual bool SetExposureTime() { return false; }
        public virtual bool SetTriggerSource() { return false; }
        public virtual void SoftwareTrig() { return; }
        public virtual void Trig() { return; }

        public virtual void Continuous() { return; }
        public virtual void StopContinuous() { return; }
        public virtual void Close() { }

        public virtual bool WaitOne()
        {

            return false;
        }

        [NotMapped]

        public Mat Mat { get; set; }
        public class CameraParams : BindableBase
        {


            private float _MinExposureTime;


            public float MinExposureTime
            {
                get { return _MinExposureTime; }
                set { SetProperty(ref _MinExposureTime, value); }
            }

            private float _MaxExposureTime;


            public float MaxExposureTime
            {
                get { return _MaxExposureTime; }
                set { SetProperty(ref _MaxExposureTime, value); }
            }



            private float _ExposureTime;
            /// <summary>
            /// Comment
            /// </summary>
            public float ExposureTime
            {
                get { return _ExposureTime; }
                set
                {
                    SetProperty(ref _ExposureTime, value);

                }
            }



            private TriggerMethod _TriggerSource = TriggerMethod.Software;
            /// <summary>
            /// Comment
            /// </summary>
            public TriggerMethod TriggerSource
            {
                get { return _TriggerSource; }
                set
                {
                    SetProperty(ref _TriggerSource, value);

                }
            }

        }
    }
}
